from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMessageBox, QLineEdit

from entity import currentuser
# from entity.currentuser import CurrentUserid
from entity.user import user
from mainwindow.rootMain import ROOTMainW
from mainwindow.studentMain import SMainW
from opsql import studentOp
# from mainWindow.managerMain import MMainW
# from mainWindow.readerMain import RMainW
# from mainWindow.rootMain import ROOTMainW
# from opsql import readerOp
from opsql.loginOp import loginstudent, loginroot
from register import Register


current_user=user(None, None)
class Login:
    def __init__(self):
        self.ui = QUiLoader().load('view/denglu.ui')
        self.ui.loginBtn.clicked.connect(self.login)
        self.ui.registerBtn.clicked.connect(self.register)
        self.ui.studentRadioBtn.setChecked(True)
        self.ui.passwordIn.setEchoMode(QLineEdit.Password)
        self.ui.rootRadioBtn.clicked.connect(self.rootRadioBtn)
        self.ui.studentRadioBtn.clicked.connect(self.studentRadioBtn)


    def login(self):
        self.username = self.ui.usernameIn.text()
        self.password = self.ui.passwordIn.text()
        current_user.username = self.username
        current_user.password = self.password
        if self.ui.rootRadioBtn.isChecked():
            if loginroot(current_user):
                self.main=ROOTMainW()
                self.main.ui.show()
            else:
                QMessageBox.warning(self.ui,'登录失败','用户名或密码错误')
        elif self.ui.studentRadioBtn.isChecked():
            if loginstudent(current_user):
                snum=studentOp.searchsnum(self.username)
                currentuser.current_userid=snum
                self.main=SMainW()
                self.main.ui.show()
            else:
                QMessageBox.warning(self.ui,'登录失败','用户名或密码错误')

    def register(self):
        self.register=Register()
        self.register.ui.show()

    def rootRadioBtn(self):
        self.ui.usernameIn.setText('root')
        self.ui.usernameIn.setReadOnly(True)

    def studentRadioBtn(self):
        self.ui.usernameIn.setText('')
        self.ui.usernameIn.setReadOnly(False)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = Login()
    window.ui.show()
    sys.exit(app.exec())